package xyz.xuminghai.javafx_learn.scene.control;

import javafx.application.Application;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.collections.FXCollections;
import javafx.collections.transformation.SortedList;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.converter.DefaultStringConverter;

import java.util.List;
import java.util.Objects;
import java.util.StringJoiner;

/**
 * 2023/5/22 21:24 星期一<br/>
 * 表格
 *
 * @author xuMingHai
 */
public class TableViewApp extends Application {

    private final VBox vBox = new VBox(30.0);


    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void init() {
        SortedList<Student> sortedList = new SortedList<>(FXCollections.observableArrayList(
                List.of(new Student(1, "张三"),
                        new Student(2, "李四"),
                        new Student(3, "王五"),
                        new Student(4, "赵六"),
                        new Student(5, "田七"),
                        new Student(6, "赵八"),
                        new Student(7, "徐九"))));
        TableView<Student> tableView = new TableView<>(sortedList);
        sortedList.comparatorProperty().bind(tableView.comparatorProperty());

        Button button = new Button("printList");
        button.setOnAction(event -> System.out.println(sortedList.getSource()));

        TableColumn<Student, Integer> idColumn = new TableColumn<>("ID");
        // 调整列宽
        idColumn.setResizable(false);
        // 调整列所在位置
        idColumn.setReorderable(false);
        idColumn.setCellValueFactory(param -> new ReadOnlyObjectWrapper<>(param.getValue().getId()));
        idColumn.setCellFactory(param -> {

            TableCell<Student, Integer> tableCell = new TableCell<>() {

                @Override
                protected void updateItem(Integer item, boolean empty) {

                    if (Objects.equals(item, getItem())) {
                        return;
                    }

                    super.updateItem(item, empty);

                    if (empty || item == null) {
                        setText(null);
                        setGraphic(null);
                    } else {
                        setText(item.toString());
                    }
                }


            };

            tableCell.setAlignment(Pos.CENTER);
            return tableCell;
        });
        tableView.getColumns().add(idColumn);

        TableColumn<Student, String> nameColumn = new TableColumn<>("Name");
        nameColumn.setSortable(false);
        // 调整列宽
        nameColumn.setResizable(false);
        // 调整列所在位置
        nameColumn.setReorderable(false);
        nameColumn.setCellValueFactory(param -> new ReadOnlyObjectWrapper<>(param.getValue().getName()));
        nameColumn.setCellFactory(param -> {
            TextFieldTableCell<Student, String> textFieldTableCell = new TextFieldTableCell<>(new DefaultStringConverter());
            textFieldTableCell.setAlignment(Pos.CENTER);
            return textFieldTableCell;
        });

        nameColumn.setOnEditCommit(event -> event.getRowValue().setName(event.getNewValue()));
        tableView.getColumns().add(nameColumn);

        tableView.setEditable(true);
        tableView.setTableMenuButtonVisible(true);


        vBox.setAlignment(Pos.CENTER);
        vBox.setFillWidth(false);
        vBox.getChildren().addAll(button, tableView);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle(getClass().getSimpleName());
        Scene scene = new Scene(vBox, 800, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private static class Student {
        private final Integer id;
        private String name;


        public Student(Integer id, String name) {
            this.id = id;
            this.name = name;
        }

        public Integer getId() {
            return id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (!(o instanceof Student student)) return false;

            if (!id.equals(student.id)) return false;
            return name.equals(student.name);
        }

        @Override
        public int hashCode() {
            int result = id.hashCode();
            result = 31 * result + name.hashCode();
            return result;
        }

        @Override
        public String toString() {
            return new StringJoiner(", ", Student.class.getSimpleName() + "[", "]")
                    .add("id=" + id)
                    .add("name='" + name + "'")
                    .toString();
        }
    }

}
